/**
 * 数据库直接操作的 api，建议正式部署时删除
 */
var router = require('express').Router();
var dbentityTool = require('../../bin/dbentity');
var ObjectID = require('mongodb').ObjectID;

var handleOID = function(obj){
    for (let key in obj){
        if (key !== undefined){
            if (typeof(obj[key]) === 'object'){
                handleOID(obj[key]);
            } else if (key === '_id' && typeof(obj[key]) === 'string' && obj[key].match(/^[a-fA-F0-9]{24}$/)){
                obj[key] = ObjectID(obj[key]);
            }
        }
    }
    return obj;
};

// find docs from database
router.post('/find', async function (req, res) {
    try {
        res.json(await dbentityTool.find(req.body.col, handleOID(JSON.parse(req.body.filter)), JSON.parse(req.body.options)));
    } catch (ex) {
        res.json(ex);
    }
});

// find docs from database
router.post('/findOne', async function (req, res) {
    try {
        res.json(await dbentityTool.findOne(req.body.col, handleOID(JSON.parse(req.body.filter))));
    } catch (ex) {
        res.json(ex);
    }
});

// save doc to database
router.post('/upsert', async function (req, res) {
    try {
        res.json(await dbentityTool.upsert(req.body.col, handleOID(JSON.parse(req.body.doc))));
    } catch (ex) {
        res.json(ex);
    }
});

// save doc
router.post('/save', async function (req, res) {
    try {
        res.json(await dbentityTool.save(req.body.col, handleOID(JSON.parse(req.body.doc))));
    } catch (ex) {
        res.json(ex);
    }
});

// save docs
router.post('/saveMany', async function (req, res) {
    try {
        res.json(await dbentityTool.saveMany(req.body.col, handleOID(JSON.parse(req.body.docs))));
    } catch (ex) {
        res.json(ex);
    }
});

// find one and update
router.post('/findOneAndUpdate', async function (req, res) {
    try {
        res.json(await dbentityTool.findOneAndUpdate(req.body.col, handleOID(JSON.parse(req.body.filter)), JSON.parse(req.body.updater)));
    } catch (ex) {
        res.json(ex);
    }
});

// find one and replace
router.post('/findOneAndReplace', async function (req, res) {
    try {
        res.json(await dbentityTool.findOneAndReplace(req.body.col, handleOID(JSON.parse(req.body.filter)), handleOID(JSON.parse(req.body.doc))));
    } catch (ex) {
        res.json(ex);
    }
});

// find one and update
router.post('/findOneAndDelete', async function (req, res) {
    try {
        res.json(await dbentityTool.findOneAndDelete(req.body.col, handleOID(JSON.parse(req.body.filter))));
    } catch (ex) {
        res.json(ex);
    }
});

// update Many
router.post('/updateMany', async function (req, res) {
    try {
        res.json(await dbentityTool.updateMany(req.body.col, handleOID(JSON.parse(req.body.filter)), JSON.parse(req.body.updater)));
    } catch (ex) {
        res.json(ex);
    }
});

// update One
router.post('/updateOne', async function (req, res) {
    try {
        res.json(await dbentityTool.updateOne(req.body.col, handleOID(JSON.parse(req.body.filter)), JSON.parse(req.body.updater)));
    } catch (ex) {
        res.json(ex);
    }
});

// delete Many
router.post('/deleteMany', async function (req, res) {
    try {
        res.json(await dbentityTool.deleteMany(req.body.col, handleOID(JSON.parse(req.body.filter))));
    } catch (ex) {
        res.json(ex);
    }
});

module.exports = router;